/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of TranscoderPerseus.
 *
 * IoUtilPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * IoUtilPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with IoUtilPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.util;

import java.io.*;

/**
 * Manage the Input/Output providing shortcuts for BufferedReader and BufferedWriter.
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class IoManager{

    /**
     * Constructor.
     */
    public IoManager(){}

    /**
     * Provide a BufferedReader based on a File.
     * Default encoding is UTF-8.
     *
     * @param filename the name of the File to read from.
     * @return the BufferedReader.
     */
    public static BufferedReader getReader(String filename){
        try{
            return getReader(filename, "UTF-8");
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Provide a BufferedReader based on a String.
     *
     * @param str the String.
     * @return the BufferedReader.
     */
    public static BufferedReader getReaderFromString(String str){
        try{
            return new BufferedReader(new StringReader(str));
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Provide a BufferedReader based on a File, with a particular encoding.
     *
     * @param filename the name of the File to read from.
     * @param encoding the encoding (e.g. UTF-8).
     * @return the BufferedReader.
     */
    public static BufferedReader getReader(String filename, String encoding){
	try{
	    return new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding));
	}catch(Exception e){
	    e.printStackTrace();
	    return null;
	}
    }

    /**
     * Provide a BufferedWriter based on a File.
     *
     * @param filename the name of the File to write to.
     * @return the BufferedWriter.
     */
    public static BufferedWriter getWriter(String filename){
        try{
            return getWriter(filename, "UTF-8");
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Provide a BufferedWriter based on a String, with an initial size.
     *
     * @param initSize initial size of the Buffer.
     * @return the BufferedWriter.
     */
    public static BufferedWriter getWriterToString(int initSize){
        try{
            return new BufferedWriter(new StringWriter(initSize));
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Provide a BufferedWriter based on a String.
     *
     * @return the BufferedWriter.
     */
    public static BufferedWriter getWriterToString(){
        try{
            return new BufferedWriter(new StringWriter());
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Provide a BufferedWriter based on a File, with a particular encoding.
     *
     * @param filename the name of the file to write to.
     * @param encoding the encoding (e.g. UTF-8).
     * @return the BufferedWriter.
     */
    public static BufferedWriter getWriter(String filename, String encoding){
	try{
	    return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), encoding));
	}catch(Exception e){
	    e.printStackTrace();
	    return null;
	}
    }

    /**
     * Provide a PrintStream based on System.out, with UTF-8 encoding.
     *
     * @return the PrintStream.
     */
    public static PrintStream getSystemOut(){
	try{
	    return new PrintStream(System.out, true, "UTF-8");
	}catch(Exception e){
	    e.printStackTrace();
	    return null;
	}
    }

}
